﻿using System.Collections.Generic;
using Game;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

[RequireComponent(typeof(SceneObjGroup))]
public class Editor_ObjData : MonoBehaviour
{

    #region Editor data field
    #if UNITY_EDITOR

    /// <summary>
    /// 是否更改绘制体的父物体
    /// </summary>
    public bool changeNewObjParent;

    [SerializeField] GameObject _newObjParent;
    /// <summary>
    /// 新建物体的新父物体，直接取即用。对于未勾选返回新父物体的，则以当前物体为父物体
    /// </summary>
    public GameObject newObjParent
    {
        get
        {
            if(changeNewObjParent && _newObjParent !=null)
            {
                return _newObjParent;
            }
            else
            {
                return  this.transform.GetChild(0).gameObject;   
            }
        }
        set
        {
            _newObjParent = value;
        }
    }

    /// <summary>
    /// 在带有什么tag的物体表面进行放置物体，默认为地形
    /// </summary>
    public string targetObjTagToPaintOn = "Terrain";                                            //在带有什么tag的物体表面进行放置物体


    /// <summary>
    /// 批量生成物体的数目Min
    /// </summary>
    public int batch_generateCount_Min = 100;

    /// <summary>
    /// 批量生成物体的数目Max
    /// </summary>
    public int batch_generateCount_Max = 200;

    /// <summary>
    /// 用于检测高度位置的地形
    /// </summary>
    public Terrain batch_terrainTarget;
    /// <summary>
    /// 生成范围X 
    /// </summary>
    public float batchGenerateRangeX = 10f;
    /// <summary>
    /// 生成范围Z
    /// </summary>
    public float batchGenerateRangeZ = 10f;
    /// <summary>
    /// 更方便的组批量：生成多少组的批量
    /// </summary>
    public int groupbatch_groupCount = 10;
    /// <summary>
    /// 对组进行批量创建时，前缀名称
    /// </summary>
    public string groupbatch_groupNamePrefix = "TerrainDetailObjs_Group_";
    /// <summary>
    /// 对组进行批量创建时，后缀序号
    /// </summary>
    public int groupbatch_groupNameIndexPostfix = 300;
    /// <summary>
    /// 对组进行批量创建时，可指定在此物体附近的范围X（将以此物体为中心的范围，任一负数则以地形为范围）
    /// </summary>
    public int groupbatch_groupGenerateRangeX = -1;
    /// <summary>
    /// 对组进行批量创建时，可指定在此物体附近的范围Z（将以此物体为中心的范围，任一负数则以地形为范围）
    /// </summary>
    public int groupbatch_groupGenerateRangeZ = -1;

    #endif
    #endregion

    internal void InitObjData()
    {
        if (this.transform.childCount <= 0)
        {
            var dlgate = new GameObject("DetailsGroup");
            dlgate.transform.SetParent(this.transform);

#if UNITY_EDITOR
            Undo.RegisterCreatedObjectUndo(dlgate, "Create delegate");
#endif
        }

        if(Editor_GetObjGroupManager()==null)
        {
            Debug.LogError("Cannot find " + typeof(SceneObjGroupManager).Name
                + ", you should set a groupManager to controll all " + typeof(SceneObjGroup).Name + " (Which handle group detail objs ");
        }
    }

    public SceneObjGroup Editor_GetSceneObjGroup()
    {
        return GetComponent<SceneObjGroup>();
    }

    static SceneObjGroupManager g_ref_groupManager;
    /// <summary>
    /// 找到GroupManager，如果不是其子物体，则将悬挂为其子物体
    /// </summary>
    /// <returns>The object group manager.</returns>
    public SceneObjGroupManager Editor_GetObjGroupManager()
    {
        //--加快查找：(已经有缓存，且此物体被正确悬挂在该物体下)
        if(g_ref_groupManager!=null)
        {
            #if UNITY_EDITOR
            if( this.transform.parent == null)          //编辑器模式下，一定要确认物体是否在其下：
            {
                Debug.LogError("You should attach this group obj under groupManager");
            }
            #endif
            return g_ref_groupManager;
        }

        //先在父物体尝试查找：
        SceneObjGroupManager refGroupObjMgr = transform.GetComponentInParent<SceneObjGroupManager>();

        if (refGroupObjMgr == null)
        {
            refGroupObjMgr = FindObjectOfType<SceneObjGroupManager>();
            if (refGroupObjMgr == null)
            {
                var obj = new GameObject("GroupMgrInstance", typeof(SceneObjGroupManager));
                refGroupObjMgr = obj.GetComponent<SceneObjGroupManager>();
#if UNITY_EDITOR
                Undo.RegisterCreatedObjectUndo(obj, "Create Instance");
#endif
            }

            //TODO:测试此处
#if UNITY_EDITOR
            Undo.RecordObject(this.transform, "GroupMgr attach");

            if (Application.isPlaying)
            {
                Debug.LogError("You should attach group obj " + this.name +"under groupManager.");
            }
#endif
            //Attach to it:
            this.transform.SetParent(refGroupObjMgr.transform);

            
        }

        g_ref_groupManager = refGroupObjMgr;
        return refGroupObjMgr;
    }

    #region UNITY Editor Function Field
    #if UNITY_EDITOR
    private void OnDrawGizmosSelected()
    {
        //显示出可绘制的区域
        Vector3 center = transform.position;
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(center, new Vector3(batchGenerateRangeX, 1, batchGenerateRangeZ));
    }
    #endif
    #endregion

}
